
//
//  PywSplashView.m
//  PywSdk
//
//  Created by finn on 15/11/23.
//  Copyright © 2015年 zero. All rights reserved.
//

#import "PywSplashView.h"
#import "UIImage+Pyw.h"
#import "UIView+Pyw.h"
#import "UILabel+Pyw.h"
#import "PywCommonManager.h"
#import "PywAppInfo.h"

#define LOGO_W [UIImage imageWithName:@"logo_start"].size.width
#define LOGO_H [UIImage imageWithName:@"logo_start"].size.height

@interface PywSplashView ()
// 朋友玩启动页
@property (nonatomic, weak)UIImageView  *iv;
@property (nonatomic, weak)UILabel      *label;
// 风趣启动页
@property (nonatomic, strong) UIImageView *fqStartIv;
/** 状态栏方向 */
@property (nonatomic, assign)UIInterfaceOrientation orientation;

@end

@implementation PywSplashView

- (instancetype)init
{
    self = [super init];
    if (self) {
        if ([PywAppInfo sharedAppInfo].appType == AppTypePYW) {
            self.backgroundColor = [UIColor whiteColor];
            
            UIImageView *iv = [[UIImageView alloc] init];
            iv.image = [UIImage imageWithName:@"logo_start"];
            iv.contentMode = UIViewContentModeCenter;
            [self addSubview:iv];
            self.iv = iv;
            
            UILabel *label = [UILabel labelWithText:@"Copyright©2016 朋友玩. All Rights Reserved."
                                      textAlignment:NSTextAlignmentCenter
                                           fontSize:12
                                          textColor:[UIColor blackColor]];
            [self addSubview:label];
            self.label = label;
            
        } else {
            [self addSubview:self.fqStartIv];
        }
    }
    return self;
}

- (void)showSplashToView:(UIView *)view{
        
    if (view == nil) {
       view = [[UIApplication sharedApplication].windows lastObject];
    }
    [view addSubview:self];
    
    [self setupOrientation];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)setupOrientation {
    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if (orientation == UIInterfaceOrientationPortrait ) {
        _orientation = UIInterfaceOrientationPortrait;
        [self verticalScreenControlsDistribution];
        
    } else if (orientation == UIInterfaceOrientationLandscapeLeft ) {
        
        _orientation = UIInterfaceOrientationLandscapeLeft;
        
        [self horizontalScreenControlsDistribution];
        
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        
        _orientation = UIInterfaceOrientationLandscapeRight;
        
        [self horizontalScreenControlsDistribution];
        
    } else {
        _orientation = UIInterfaceOrientationPortraitUpsideDown;
        [self verticalScreenControlsDistribution];
    }
}

// 屏幕方向改变时调用
- (void)orientationChange {
    
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    
    if (_orientation == orientation) return; //方向一致则不做操作
    
    [self screenRotationMethod:orientation];
    
}
- (void)screenRotationMethod:(UIInterfaceOrientation) orientation
{
    
    if (orientation == UIInterfaceOrientationPortrait
        || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        _orientation = orientation;
        
        [self verticalScreenControlsDistribution];
    }else{
        _orientation = orientation;
        
        [self horizontalScreenControlsDistribution];
    }
}

// 竖屏布局
- (void)verticalScreenControlsDistribution {

    CGFloat width = [UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height ? [UIScreen mainScreen].bounds.size.width : [UIScreen mainScreen].bounds.size.height;
    CGFloat height = [UIScreen mainScreen].bounds.size.height > [UIScreen mainScreen].bounds.size.width ? [UIScreen mainScreen].bounds.size.height : [UIScreen mainScreen].bounds.size.width;
    self.frame = CGRectMake(0, 0, width, height);
    
    if ([PywAppInfo sharedAppInfo].appType == AppTypePYW) {
        CGFloat padding = 5;
        CGFloat dotW = 10;
        CGFloat ivWidth = 126;
        CGFloat ivHeight = 43.5;
        if (IS_IPAD || IS_IPHONE_6P) {
            ivWidth = ivWidth * 1.5;
            ivHeight = ivHeight * 1.5;
        }
        
        self.iv.frame = CGRectMake(0, 0, ivWidth, ivHeight);
        self.iv.center = CGPointMake(width * 0.5, height * 0.3);
        
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:12];
        NSString *content = @"Copyright©2016 朋友玩. All Rights Reserved.";
        CGSize size =[content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}];
        CGFloat label_W = size.width;
        CGFloat label_H = size.height;
        self.label.frame = CGRectMake((SCREEN_WIDTH - label_W) * 0.5, SCREEN_HEIGHT - 30 - label_H, label_W, label_H);
    } else {
        self.fqStartIv.image = [UIImage imageWithName:@"bg_default_port"];
        self.fqStartIv.frame = self.frame;
    }
}

// 横屏布局
- (void)horizontalScreenControlsDistribution {
    
    CGFloat width = [UIScreen mainScreen].bounds.size.width <[UIScreen mainScreen].bounds.size.height ? [UIScreen mainScreen].bounds.size.height : [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height >[UIScreen mainScreen].bounds.size.width ? [UIScreen mainScreen].bounds.size.width : [UIScreen mainScreen].bounds.size.height;
    self.frame = CGRectMake(0, 0, width, height);
    
    if ([PywAppInfo sharedAppInfo].appType == AppTypePYW) {
        
        CGFloat padding = 5;
        CGFloat dotW = 10;
        
        CGFloat ivWidth = 126;
        CGFloat ivHeight = 43.5;
        if (IS_IPAD || IS_IPHONE_6P) {
            ivWidth = ivWidth * 1.5;
            ivHeight = ivHeight * 1.5;
        }
        
        self.iv.frame = CGRectMake(0, 0, ivWidth, ivHeight);
        self.iv.center = CGPointMake(width * 0.5, height * 0.3);
        
        CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:12];
        NSString *content = @"Copyright©2016 朋友玩. All Rights Reserved.";
        CGSize size =[content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]}];
        CGFloat label_W = size.width;
        CGFloat label_H = size.height;
        self.label.frame = CGRectMake((SCREEN_WIDTH - label_W) * 0.5, SCREEN_HEIGHT - 30 - label_H, label_W, label_H);
    } else {
        self.fqStartIv.image = [UIImage imageWithName:@"bg_default_land"];
        self.fqStartIv.frame = self.frame;
    }
    
}

- (void)dealloc{
        
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (UIImageView *)fqStartIv{
    if (!_fqStartIv) {
        _fqStartIv = [[UIImageView alloc] init];
    }
    return _fqStartIv;
}

@end
